package com.yamaha.bikedesalon;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yamaha.bikedesalon.data.BrandInfo;
import com.yamaha.bikedesalon.data.CategoryInfo;
import com.yamaha.bikedesalon.data.DealerInfo;
import com.yamaha.bikedesalon.data.ModelInfo;
import com.yamaha.bikedesalon.data.ProvinceInfo;
import com.yamaha.bikedesalon.data.TrustStoreInfo;
import com.yamaha.bikedesalon.postItem.BodyCommentItem;
import com.yamaha.bikedesalon.postItem.BodyItem;
import com.yamaha.bikedesalon.postItem.BodyPostPartItem;
import com.yamaha.bikedesalon.postItem.FooterItem;
import com.yamaha.bikedesalon.postItem.FooterPostPartItem;
import com.yamaha.bikedesalon.postItem.HeaderItem;
import com.yamaha.bikedesalon.postItem.HeaderPostDetailItem;
import com.yamaha.bikedesalon.postItem.HeaderPostPartItem;
import com.yamaha.bikedesalon.postItem.Item;

public final class DataManager {

	private static final String DEBUG_TAG = "DataManager";

	public static List<BrandInfo> brandInfoList = new ArrayList<BrandInfo>();
	public static List<CategoryInfo> categoryInfoList = new ArrayList<CategoryInfo>();
	public static List<DealerInfo> dealerInfoList = new ArrayList<DealerInfo>();
	public static List<ModelInfo> modelInfoList = new ArrayList<ModelInfo>();
	public static List<ProvinceInfo> provinceInfoList = new ArrayList<ProvinceInfo>();
	public static List<TrustStoreInfo> trustStoreInfoList = new ArrayList<TrustStoreInfo>();

	public static void SetInitialData(JSONObject object) {

		try {
			JSONArray brands = object.getJSONArray("brand");
			for (int i = 0; i < brands.length(); i++) {
				brandInfoList.add(new BrandInfo(brands.getJSONObject(i)));
			}

			JSONArray models = object.getJSONArray("model");
			for (int i = 0; i < models.length(); i++) {
				modelInfoList.add(new ModelInfo(models.getJSONObject(i)));
			}

			JSONArray provinces = object.getJSONArray("province");
			for (int i = 0; i < provinces.length(); i++) {
				provinceInfoList.add(new ProvinceInfo(provinces.getJSONObject(i)));
			}

			JSONArray dealers = object.getJSONArray("dealer");
			for (int i = 0; i < dealers.length(); i++) {
				dealerInfoList.add(new DealerInfo(dealers.getJSONObject(i)));
			}

			JSONArray trustStores = object.getJSONArray("trust_store");
			for (int i = 0; i < trustStores.length(); i++) {
				trustStoreInfoList.add(new TrustStoreInfo(trustStores.getJSONObject(i)));
			}

			JSONArray categories = object.getJSONArray("category");
			for (int i = 0; i < categories.length(); i++) {
				categoryInfoList.add(new CategoryInfo(categories.getJSONObject(i)));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static List<String> GetBrandList() {
		List<String> result = new ArrayList<String>();

		for (BrandInfo item : brandInfoList) {
			result.add(item.Name);
		}
		return result;
	}
	public static String GetBrandIdByName(String name) {
		for (BrandInfo item : brandInfoList) {
			// if (item.Name == name) {
			if (item.Name.compareTo(name) == 0) {
				return item.Id;
			}
		}
		return null;
	}
	public static String GetBrandNameById(String id) {
		for (BrandInfo item : brandInfoList) {
			// if (item.Id == id) {
			if (item.Id.compareTo(id) == 0) {
				return item.Name;
			}
		}
		return null;
	}
	// -------------------------------------------------------------------------------------
	public static List<String> GetModelList() {
		List<String> result = new ArrayList<String>();

		for (ModelInfo item : modelInfoList) {
			result.add(item.Name);
		}
		return result;
	}
	public static String GetModelIdByName(String name) {
		for (ModelInfo item : modelInfoList) {
			// if (item.Name == name) {
			if (item.Name.compareTo(name) == 0) {
				return item.Id;
			}
		}
		return null;
	}
	public static String GetModelNameById(String id) {
		for (ModelInfo item : modelInfoList) {
			// if (item.Id == id) {
			if (item.Id.compareTo(id) == 0) {
				return item.Name;
			}
		}
		return null;
	}
	// -------------------------------------------------------------------------------------
	public static List<String> GetCategoryList() {
		List<String> result = new ArrayList<String>();

		for (CategoryInfo item : categoryInfoList) {
			result.add(item.Name);
		}
		return result;
	}
	public static String GetCategoryIdByName(String name) {
		for (CategoryInfo item : categoryInfoList) {
			// if (item.Name == name) {
			if (item.Name.compareTo(name) == 0) {
				return item.Id;
			}
		}
		return null;
	}
	public static String GetCategoryNameById(String id) {
		for (CategoryInfo item : categoryInfoList) {
			// if (item.Id == id) {
			if (item.Id.compareTo(id) == 0) {
				return item.Name;
			}
		}
		return null;
	}
	// -------------------------------------------------------------------------------------
	public static List<String> GetDealerList() {
		List<String> result = new ArrayList<String>();

		for (DealerInfo item : dealerInfoList) {
			result.add(item.Name);
		}
		return result;
	}
	public static String GetDealerIdByName(String name) {
		for (DealerInfo item : dealerInfoList) {
			// if (item.Name == name) {
			if (item.Name.compareTo(name) == 0) {
				return item.UserId;
			}
		}
		return null;
	}
	public static String GetDealerNameById(String id) {
		for (DealerInfo item : dealerInfoList) {
			// if (item.UserId == id) {
			if (item.UserId.compareTo(id) == 0) {
				return item.Name;
			}
		}
		return null;
	}
	// -------------------------------------------------------------------------------------

	public static List<String> GetProvinceList() {
		List<String> result = new ArrayList<String>();

		for (ProvinceInfo item : provinceInfoList) {
			result.add(item.Name);
		}
		return result;
	}
	public static String GetProvinceIdByName(String name) {
		for (ProvinceInfo item : provinceInfoList) {
			// if (item.Name == name) {
			if (item.Name.trim().compareTo(name.trim()) == 0) {
				return item.Id;
			}
		}
		return null;
	}
	public static String GetProvinceNameById(String id) {
		for (ProvinceInfo item : provinceInfoList) {
			// if (item.Id == id) {
			if (item.Id.compareTo(id) == 0) {
				return item.Name;
			}
		}
		return null;
	}
	// -------------------------------------------------------------------------------------

	public static List<Item> ConvertToPostPartItems(ImageLoader imageLoader, JSONArray objects) {
		List<Item> items = new ArrayList<Item>();
		if (objects.length() == 0) {
			return items;
		}
		items.add(new HeaderPostPartItem());
		for (int i = 0; i < objects.length(); i++) {

			try {
				// items.add(new BodyItem(imageLoader,
				// objects.getJSONObject(i)));
				items.add(new BodyPostPartItem(imageLoader, objects.getJSONObject(i)));
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		items.add(new FooterPostPartItem());

		return items;
	}

	public static List<Item> ConvertToPostItems(ImageLoader imageLoader, String header, JSONArray objects) {
		List<Item> items = new ArrayList<Item>();
		if (objects.length() == 0) {
			return items;
		}
		items.add(new HeaderItem(header, ""));
		for (int i = 0; i < objects.length(); i++) {

			try {
				items.add(new BodyItem(imageLoader, objects.getJSONObject(i)));
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		items.add(new FooterItem());

		return items;
	}

	public static List<Item> ConvertToNotificationItem(ImageLoader imageLoader, JSONArray objects) {
		List<Item> items = new ArrayList<Item>();

		if (objects.length() == 0) {
			return items;
		}

		for (int i = 0; i < objects.length(); i++) {

			try {
				String strDate = objects.getJSONObject(i).getString("date");
				items.add(new HeaderItem("Update", strDate));

				JSONArray arr = objects.getJSONObject(i).getJSONArray("item");

				for (int j = 0; j < arr.length(); j++) {

					items.add(new BodyItem(imageLoader, arr.getJSONObject(i)));
				}
				items.add(new FooterItem());

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return items;
	}

	public static List<Item> ConvertToPostDetailItem(Context c, ImageLoader imageLoader, JSONObject postInfo, JSONArray list) {
		// TODO Auto-generated method stub
		List<Item> items = new ArrayList<Item>();

		items.add(new HeaderPostDetailItem(c, imageLoader, postInfo));

		// add comment
		for (int i = 0; i < list.length(); i++) {

			try {
				items.add(new BodyCommentItem(imageLoader, list.getJSONObject(i)));
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return items;
	}

}
